﻿using Blog.Core.Domain.Model.User;
using Blog.Core.IServices.IUser;
using Blog.Core.IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Blog.Core.DTO.ViewModel.User;
using Blog.Core.Services.User;
using Blog.Core.DTO;
using Blog.Core.DTO.ViewModel;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;

namespace Blog.Core.Api.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Authorize]
    [Route("api/[controller]/[action]")]//路由的配置
    [ApiController]
    public class RoleController : BaseController<Roles>
    {
        private readonly IRolesServices roleServices;
        public RoleController(IBaseService<Roles> baseService, IRolesServices roleServices) : base(baseService)
        {
            this.roleServices = roleServices;
        }
        /// <summary>
        /// 根据角色名称查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("name")]
        public async Task<IActionResult> GetAll(string? name)
        {
            var list = await roleServices.GetAll();
            //ResponseResultDTO<PageDTO<>>
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.RoleName.Contains(name)).ToList();
            }
            list= list.Where(x => x.Delete == 0).ToList();
            return Ok(list);
        }
        /// <summary>
        /// 角色分配权限时角色的显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllList()
        {
            ResponseResultDTO<List<Roles>> response = new ResponseResultDTO<List<Roles>>();
            var list =await roleServices.GetAll();
            list = list.Where(x => x.Delete == 0).ToList();
            if (list==null)
            {
                response.Code = ResultStatusCode.HANDLEFAIL;
                response.Message = "请求失败";
                return Ok(response);
            }
            response.Code = ResultStatusCode.SUCCESS;
            response.Message = "请求成功";
            response.Data = list;
            return Ok(response);
        }
        /// <summary>
        /// 角色列表分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRoleInfo(string? name, int pageindex = 1, int pagesize = 5)
        {
            ResponseResultDTO<PageDTO<GetRolePowerDTO>> response = new ResponseResultDTO<PageDTO<GetRolePowerDTO>>();
            var data = await roleServices.GetRoleInfo(name,pageindex, pagesize);
            response.Code = ResultStatusCode.SUCCESS;
            response.Message = "成功";
            response.Data = data;
            return Ok(response);

        }
        /// <summary>
        /// 批量修改角色状态
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> PutRole(string ids)
        {
            return Ok(await roleServices.PutRole(ids));
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelectRole(string ids)
        {
            return Ok(await roleServices.DelectRole(ids));
        }
        /// <summary>
        /// 角色录入
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostRoleInfo(Roles entity)
        {
            return Ok(await roleServices.PostRoleInfo(entity));
        }
    }
}
